<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\WorkLogic;
use Illuminate\Http\Request;

//流程相关
class WorkController extends Controller
{



    //流程列表
    public function workList(Request $request)
    {
        $rules = array(
            'name'=>"present|nullable|string",
            'page'=>"present|numeric|min:1",
            'pagesize'=>"present|numeric|min:10",
            'status'=>'present|numeric|nullable',
            'type'=>'sometimes|nullable|numeric'
        );

        $this->verify($request->all(),$rules);
        try{
            $logic = new WorkLogic();
            $list = $logic->getList($request->all());
            return ret_json('ok','ok',$list);
        }catch(\Exception $e){
            return ret_json('err',$e->getMessage());
        }
    }

    //添加流程
    public function addWork(Request $request)
    {
        $rules = [
            'name'=>'required|string',
            'desc'=>'required|string',
            'position'=>'required|array',
        ];

        $this->verify($request->all(),$rules);

        try{
            $logic = new WorkLogic();

            $result = $logic->addWork($request->all());

            //dd($logic->saveWorkLink(1,[1,2,3,4]));
            $state = $result ? 'ok' : 'err';
            $msg = $result ? '添加成功' : '添加失败';
            ret_json($state,$msg);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //流程详情
    public function workDetail(Request $request)
    {

        $rules = ['id'=>'required|exists:work'];

        $this->verify($request->all(),$rules);

        try{
            $logic = new WorkLogic();
            $data = $logic->getWorkDetail($request->all());
            ret_json('ok','请求成功',$data);
        }catch (\Exception $e){
           ret_json('err',$e->getMessage());
        }
    }

    //修改流程
    public function editWork(Request $request)
    {
        $rules = [
            'id'=>'required|exists:work',
            'name'=>'present|nullable|string',
            'desc'=>'present|nullable|string',
            'workList'=>'required|array',
            'status'=>'present|nullable'
        ];

        $this->verify($request->all(),$rules);

        try{
            $logic = new WorkLogic();
            $result = $logic->editWorkInfo($request->all());
            if($result) ret_json('ok','流程修改成功');
            ret_json('err','流程修改失败');
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }

    }

   //删除流程
    public function delWork(Request $request)
    {
        try{
            $rules = ['id'=>'required|exists:work'];
            $this->verify($request->all(),$rules);
            $params = $this->mergeRequest($request);
            if($params['roleid'] != 95) throw new \Exception('请联系超级管理员');
            (new WorkLogic())->delWork($params);
            ret_json('ok','删除成功');
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
}
